
package com.foodcommunity.controller;

import com.foodcommunity.common.Result;
import com.foodcommunity.dto.CommentDTO;
import com.foodcommunity.entity.Comment;
import com.foodcommunity.entity.User;
import com.foodcommunity.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping
    public Result<Comment> addComment(@Valid @ModelAttribute CommentDTO commentDTO, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        return commentService.addComment(commentDTO, user.getId());
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteComment(@PathVariable Long id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        return commentService.deleteComment(id, user.getId());
    }

    /**
     * 获取帖子评论列表
     */
    @GetMapping("/post/{postId}")
    public Result<List<Comment>> getPostComments(@PathVariable Long postId) {
        List<Comment> comments = commentService.getPostComments(postId);
        return Result.success(comments);
    }

    /**
     * 分页获取帖子评论
     */
    @GetMapping("/post/{postId}/page")
    public Result<Map<String, Object>> getPostCommentsWithPage(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        List<Comment> comments = commentService.getPostCommentsWithPage(postId, page, size);
        int total = commentService.getPostCommentCount(postId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("comments", comments);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        
        return Result.success(result);
    }

    /**
     * 获取评论详情
     */
    @GetMapping("/{id}")
    public Result<Comment> getCommentDetail(@PathVariable Long id) {
        return commentService.getCommentDetail(id);
    }

    /**
     * 获取子评论列表
     */
    @GetMapping("/{id}/replies")
    public Result<List<Comment>> getChildComments(@PathVariable Long id) {
        List<Comment> childComments = commentService.getChildComments(id);
        return Result.success(childComments);
    }

    /**
     * 获取用户的评论列表
     */
    @GetMapping("/user")
    public Result<List<Comment>> getUserComments(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error("请先登录");
        }
        List<Comment> comments = commentService.getUserComments(user.getId());
        return Result.success(comments);
    }
} 